package com.rongshu.view.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.view.home.entity.CatalogInfo;
import com.rongshu.view.home.listener.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CatalogAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;

    private List<CatalogInfo.ResponseBean> catalogInfoList = null;

    //interface
    private OnItemClickListener mOnItemClickListener = null;

    public CatalogAdapter(Context context) {
        this.mContext = context;
    }

    public void setCatalogInfoList(List<CatalogInfo.ResponseBean> list) {
        this.catalogInfoList = list;
    }

    public List<CatalogInfo.ResponseBean> getCatalogInfoList() {
        return this.catalogInfoList;
    }

    public void addCatalogInfoList(List<CatalogInfo.ResponseBean> list) {
        this.catalogInfoList.addAll(list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_catalog_item_view, parent, false);
        return new CatalogViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        CatalogInfo.ResponseBean bean = catalogInfoList.get(position);
        CatalogViewHolder viewHolder = (CatalogViewHolder) holder;
        viewHolder.txt_article_title.setText(bean.getTitle());
        viewHolder.txt_article_data.setText(DateUtils.ConvertDate(bean.getCheck_datetime()));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (catalogInfoList == null) {
            return 0;
        }
        return catalogInfoList.size();
    }

    public static class CatalogViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_article_title)
        TextView txt_article_title;
        @BindView(R.id.txt_article_heat)
        TextView txt_article_heat;
        @BindView(R.id.txt_article_data)
        TextView txt_article_data;

        public CatalogViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //set listener
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
